/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.locationbasedagent;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class PhotoViewerActivity extends Activity  implements OnItemClickListener {

	private ImageAdapter mImageAdapter;
	private ImageView mSelectedImage;
	private Button mViewPreviousButton;
	private Button mViewNextButton;
	private Button mBackButton;
	private static int mCurrentImageIndex;
	private Gallery gallery;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photoviewer);
        
        Bundle bundle = getIntent().getExtras();
        
        String[] photoURLs = {
        		bundle.getString("photo1"),
        		bundle.getString("photo2"),
        		bundle.getString("photo3"),
        		bundle.getString("photo4"),
        		bundle.getString("photo5")
        };

	    mSelectedImage = (ImageView) findViewById(R.id.imageView1);
	    gallery = (Gallery) findViewById(R.id.gallery);
	    mImageAdapter = new ImageAdapter(this);
	    mImageAdapter.setImageURLs(photoURLs);
	    gallery.setAdapter(mImageAdapter);
		mImageAdapter.getImageDownloader().download(mImageAdapter.getItem(0), mSelectedImage);
		mCurrentImageIndex = 0;
	    gallery.setOnItemClickListener(this);
	    
	    mViewPreviousButton = (Button) findViewById(R.id.button1);
	    mViewPreviousButton.setOnClickListener(mViewPreviousListener);
	    mViewNextButton = (Button) findViewById(R.id.button2);
	    mViewNextButton.setOnClickListener(mViewNextListener);
	    mBackButton = (Button) findViewById(R.id.backbutton);
	    mBackButton.setOnClickListener(mBackButtonListener);

	}
	    
    private OnClickListener mViewPreviousListener = new OnClickListener() {
        public void onClick(View v) {
        	mCurrentImageIndex = (mCurrentImageIndex == 0) ? 0 : mCurrentImageIndex - 1;
    		mImageAdapter.getImageDownloader().download(mImageAdapter.getItem(mCurrentImageIndex), mSelectedImage);
        	gallery.setSelection(mCurrentImageIndex);
        }
    };
    
    private OnClickListener mViewNextListener = new OnClickListener() {
        public void onClick(View v) {        	
           	mCurrentImageIndex = (mCurrentImageIndex == mImageAdapter.getCount()-1) ? mCurrentImageIndex : mCurrentImageIndex + 1;
    		mImageAdapter.getImageDownloader().download(mImageAdapter.getItem(mCurrentImageIndex), mSelectedImage);
        	gallery.setSelection(mCurrentImageIndex);
        }
    };
    
    private OnClickListener mBackButtonListener = new OnClickListener() {
        public void onClick(View v) {        	
           	finish();
        }
    };
    
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		String imageString = mImageAdapter.getItem(position);
		mImageAdapter.getImageDownloader().download(imageString, mSelectedImage);
		mCurrentImageIndex = position;
	}


}
